<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="other">
          <el-form-item label="指派人" size="small">
            <el-input v-model="searchForm.userName"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="other" v-if="roleType===0 || roleType===1 || roleType===5 || roleType===9 || roleType===15">
          <el-form-item label="报名人" size="small">
            <el-input v-model="searchForm.applicant"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="项目名称" size="small">
            <el-input v-model="searchForm.projectName"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="状态" size="small" class="item-inline">
            <el-select v-model="searchForm.isReport" placeholder="请选择">
              <el-option value="" label="全部"></el-option>
              <el-option
                v-for="item in isReportList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="性质" size="small" class="item-inline">
            <el-select v-model="searchForm.property" placeholder="请选择">
              <el-option value="" label="全部"></el-option>
              <el-option
                v-for="item in global.propertyList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
<!--      <el-button type="primary" size="small" icon="el-icon-plus" @click="addEntry()">添加</el-button>-->
      <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="440px">
        <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="80px">
          <el-form-item label="性质" size="small">
            <el-radio-group v-model="teamRow.property" @change="projectNameBlur">
              <el-radio
                v-for="item in global.propertyList"
                :label="item.value">{{item.label}}
              </el-radio>
            </el-radio-group>
            <div class="el-form-item__error">
              请确认所报备的项目性质!
            </div>
          </el-form-item>
          <el-form-item label="项目名称" size="small">
            <el-input type="text" v-model="teamRow.projectName" @blur="projectNameBlur" :disabled="dialogType===0"
                      style="width: 220px"></el-input>
          </el-form-item>
          <el-form-item prop="reportTime" label="时间" size="small">
            <el-date-picker
              v-model="teamRow.reportTime"
              type="datetime"
              value-format="timestamp"
              placeholder="选择日期"
              :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="来源" size="small">
            <el-select v-model="teamRow.source" placeholder="请选择">
              <el-option
                v-for="item in  global.sourceList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="甲方名称" size="small">
            <el-input type="text" v-model="teamRow.partyA" style="width: 220px"></el-input>
          </el-form-item>
          <el-form-item label="报名人" size="small">
            <el-input type="text" v-model="teamRow.applicant" style="width: 220px"></el-input>
          </el-form-item>
          <el-form-item label="规模" size="small">
            <el-input type="text" v-model="teamRow.projectScale" style="width: 220px"></el-input>
          </el-form-item>
          <el-form-item label="专业" size="small" class="item-inline">
            <el-select v-model="teamRow.nature" placeholder="请选择">
              <el-option
                v-for="item in parentProfessionList"
                :key="item.professionId"
                :label="item.professionName"
                :value="item.professionId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="性质小类" size="small">
            <el-input type="text" v-model="teamRow.characters" style="width: 220px"></el-input>
          </el-form-item>
          <el-form-item label="造价" size="small">
            <el-input type="number" v-model.number="teamRow.projectCost">
              <span slot="suffix">万元</span>
            </el-input>
          </el-form-item>
          <el-form-item label="备注说明" size="small">
            <el-input type="textarea" v-model="teamRow.remark" style="min-height: 100px"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click :disabled="isReport">确定</el-button>
          <el-button size="small" @click="outerVisible = false">取消</el-button>
        </div>
      </el-dialog>
      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="roleType===0 || roleType===1 || roleType===5 || roleType===9 || roleType===15?colConfigs:noColConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
    >
      <el-table-column
        slot="opt"
        fixed="right"
        label="操作"
        width="150">
        <template slot-scope="scope">
<!--          <el-popover-->
<!--            placement="top"-->
<!--            width="160"-->
<!--            :ref="`popover-1${scope.$index}`"-->
<!--            v-if="scope.row.isReport === 3 && scope.row.userId === userId"-->
<!--          >-->
<!--            <p>确定报备吗?</p>-->
<!--            <div style="text-align: right; margin: 0">-->
<!--              <el-button size="mini" type="text" @click="$refs[`popover-1${scope.$index}`].doClose()">取消</el-button>-->
<!--              <el-button type="primary" size="mini" @click.native.prevent="report(scope,1)">确定</el-button>-->
<!--            </div>-->
<!--            <el-button type="text" size="small" slot="reference" v-popover:popover-1${scope.$index}>报备</el-button>-->
<!--          </el-popover>-->
<!--          <el-popover-->
<!--            placement="top"-->
<!--            width="250"-->
<!--            height="200"-->
<!--            :ref="`popover-2${scope.$index}`"-->
<!--            v-if="scope.row.isReport === 3 && scope.row.userId === userId"-->
<!--            @hide="popoverHide"-->
<!--          >-->
<!--            <p>退回原因:</p>-->
<!--            <p>-->
<!--              <el-input type="textarea" v-model="description"></el-input>-->
<!--            </p>-->
<!--            <div style="text-align: right; margin: 0">-->
<!--              <el-button size="mini" type="text" @click="$refs[`popover-2${scope.$index}`].doClose()">取消</el-button>-->
<!--              <el-button type="primary" size="mini" @click.native.prevent="report(scope,2)">确定</el-button>-->
<!--            </div>-->
<!--            <el-button type="text" size="small" slot="reference" v-popover:popover-2${scope.$index}>退回</el-button>-->
<!--          </el-popover>-->
          <el-button
            @click.native.prevent="editEntry(scope.row.reportId)"
            type="text"
            size="small"
            v-if="scope.row.createdId === userId"
          >
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
            v-if="scope.row.createdId === userId"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import {
    getList,
    addProjectReport,
    updateProjectReport,
    delProjectReport,
    selectProjectReport,
    getParentProfessionList,
    isReport
  } from '@/api/projectreport1'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const reportTime = {
    props: ['colConfig'],
    filters: {
      formatDate(time) {
        if (time) {
          var date = new Date(time)
          return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
        }
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        {{ scope.row[colConfig.prop] | formatDate }}
      </span>
  </el-table-column>
  `
  }

  const formatter = function(row, column) {
    if (row[column.property]) {
      let obj = {}
      obj = global[`${column.property}List`].find((item) => {
        return item.value === row[column.property] // 筛选出匹配数据
      })
      return obj.label
    }
  }

  const isReportComponent = {
    props: ['colConfig'],
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        <div v-if="scope.row.isReport===1" style="color: green">已报备</div>
        <div v-else-if="scope.row.isReport===2" style="color: blue">已退回</div>
        <div v-else-if="scope.row.isReport===3" style="color: red">未报备</div>
      </span>
  </el-table-column>
  `
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'projectName', label: '项目名称', width: '350' },
          { prop: 'reportTime', label: '时间', width: '150', component: reportTime },
          { prop: 'isReport', label: '状态', width: '80', component: isReportComponent },
          { prop: 'source', label: '来源', width: '80', formatter: formatter },
          { prop: 'partyA', label: '甲方名称', width: '230' },
          { prop: 'userName', label: '指派人', width: '80' },
          { prop: 'applicant', label: '报名人', width: '80' },
          { prop: 'projectScale', label: '规模', width: '80' },
          { prop: 'property', label: '性质', width: '80', formatter: formatter },
          { prop: 'professionName', label: '专业', width: '80' },
          { prop: 'characters', label: '性质小类', width: '120' },
          { prop: 'projectCost', label: '造价(万元)', width: '80' },
          { slot: 'opt' }
        ],
        noColConfigs: [
          { prop: 'projectName', label: '项目名称', width: '350' },
          { prop: 'reportTime', label: '时间', width: '150', component: reportTime },
          { prop: 'isReport', label: '状态', width: '80', component: isReportComponent },
          { prop: 'source', label: '来源', width: '80', formatter: formatter },
          { prop: 'partyA', label: '甲方名称', width: '230' },
          { prop: 'userName', label: '指派人', width: '80' },
          { prop: 'projectScale', label: '规模', width: '80' },
          { prop: 'property', label: '性质', width: '80', formatter: formatter },
          { prop: 'professionName', label: '专业', width: '80' },
          { prop: 'characters', label: '性质小类', width: '120' },
          { prop: 'projectCost', label: '造价(万元)', width: '80' },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        listLoading: true,
        dialogTitle: '新增项目报备',
        dialogType: 0,
        teamRow: {
          property: 1
        },
        teamRowClear: {
          property: 1
        },
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        innerVisible: false,
        pca: pca,
        rules: {
          payTime: [
            { required: true, message: '请选择到付日期', trigger: 'change' }
          ],
          receiptPople: [
            { required: true, message: '请输入收件人', trigger: 'change' }
          ],
          paymentMethod: [
            { required: true, message: '请选择寄件类型', trigger: 'change' }
          ]
        },
        visible: false,
        parentProfessionList: [],
        isReport: false,
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() < Date.now() - 8.64e7
          }
        },
        description: '', // 退回原因
        isReportList: [
          { value: 1, label: '已报备' },
          { value: 2, label: '已退回' },
          { value: 3, label: '未报备' }
        ]
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.getParentProfessionList()
      this.fetchData()
    },
    computed: {
      global() {
        return global
      },
      roleType() {
        return this.$store.state.user.roleType
      },
      userId() {
        return this.$store.state.user.userId
      },
      userName() {
        return this.$store.state.user.userName
      },
      teamId() {
        return this.$store.state.user.teamId
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      getParentProfessionList() {
        getParentProfessionList().then(response => {
          this.parentProfessionList = response.responseBody
          this.parentProfessionList.forEach((value, index) => {
            if (value.professionName === 'EPC') {
              this.parentProfessionList.splice(index, 1)
            }
          })
          console.log(this.parentProfessionList)
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      addEntry() {
        this.outerVisible = true
        this.isReport = false
        this.dialogTitle = '新增项目报备'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.dialogType = 1
      },
      editEntry(reportId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.isReport = false
        this.dialogTitle = '修改项目报备'
        this.dialogType = 0
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        const params = {
          reportId: reportId
        }
        selectProjectReport(params).then(response => {
          this.teamRow = response.responseBody
        })
      },
      submitForm(formName) {
        const params = {
          isReport: this.teamRow.isReport,
          projectName: this.teamRow.projectName,
          reportTime: this.teamRow.reportTime,
          source: this.teamRow.source,
          partyA: this.teamRow.partyA,
          applicant: this.teamRow.applicant || '',
          projectScale: this.teamRow.projectScale || '',
          property: this.teamRow.property,
          nature: this.teamRow.nature,
          characters: this.teamRow.characters || '',
          projectCost: this.teamRow.projectCost || 0,
          remark: this.teamRow.remark || ''
        }
        if (!this.dialogType) {
          params.reportId = this.teamRow.reportId
        } else {
          params.userId = this.userId
          params.userName = this.userName
          params.teamId = this.teamId
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              addProjectReport(params).then(response => {
                console.log(this.dialogType)
                this.outerVisible = false
                this.fetchData()
              })
            } else {
              updateProjectReport(params).then(response => {
                this.outerVisible = false
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      report(scope, type) {
        this.$refs[`popover-${type}${scope.$index}`].doClose()
        const params = {
          reportId: scope.row.reportId,
          createdId: scope.row.createdId,
          projectName: scope.row.projectName,
          description: this.description,
          isReport: type
        }
        updateProjectReport(params).then(response => {
          this.fetchData()
        })
      },
      deleteRow(scope) {
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          reportId: scope.row.reportId,
          isDel: 1
        }
        delProjectReport(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      projectNameBlur() {
        const params = {
          property: this.teamRow.property,
          projectName: this.teamRow.projectName
        }
        isReport(params).then(response => {
          if (response.resultCode === 3) {
            this.isReport = true
          } else {
            this.isReport = false
          }
        })
      },
      popoverHide() {
        this.description = ''
      }
    },
    mounted() {
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }

    .table-btn {
      margin-bottom: 10px;

      & > button {
        margin: 0 20px;
      }

      .el-dialog {
        textarea {
          margin-top: 10px;
        }

        .el-checkbox {
          margin-right: 20px;
        }

        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  .el-dialog {
    width: 440px;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
